#include <string.h>
#include <stdlib.h>
#include "tools.h"

int isBlank(char c) {
    return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\r' || c == '\f'; 
}

int isDigit(char c) {
    return c >= '0' && c <= '9'; 
}

int isNonZeroDigit(char c) {
    return c >= '1' && c <= '9';
}

int isOctalDigit(char c) {
    return c >= '0' && c <= '7';
}

int isHexDigit(char c) {
    return c >= '0' && c<= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';
}

int isLetter(char c) {
    return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'; 
}

int isUnderLine(char c) {
    return c == '_'; 
}

int isNumber(char arr[]) {
    return strncmp(hold, "DecNumber", 9)==0 || strncmp(hold, "OctNumber", 9)==0 || strncmp(hold, "HexNumber", 9)==0;
}

void slice(char str[], int start, int end) {
    char temp[MAXLEN];
    memset(temp, '\0', sizeof(temp));
    for (int i=start; i<=end; i++) {
        temp[i-start] = str[i];
    }
    memset(hold, '\0', sizeof(hold));
    for (int i=0; i<strlen(temp); i++) {
        hold[i] = temp[i];
    }
}

int D2D(char arr[]) {
    int temp = 0;
    for(int i=0; i<strlen(arr); i++) {
        temp = temp * 10 + arr[i] - '0';
    }
    return temp;
}

int O2D(char arr[]) {
    int temp = 0;
    for(int i=0; i<strlen(arr); i++) {
        temp = temp * 8 + arr[i] - '0';
    }
    return temp;
}

int H2D(char arr[]) {
    int temp = 0;
    for(int i=0; i<strlen(arr); i++) {
        if (arr[i]>='0' && arr[i] <= '9')
            temp = temp * 16 + arr[i] - '0';
        else if (arr[i]>='a' && arr[i] <='f') {
            temp = temp * 16 + arr[i] - 'a' + 10;
        }
        else {
            temp = temp * 16 + arr[i] - 'A' + 10;
        }   
    }
    return temp;
}